// -----------------------------------------------------------------------
// <copyright file="AddEditContactPerson.aspx.cs" company="Imar.Spaanjaars.Com">
//   Copyright 2008 - 2009 - Imar.Spaanjaars.Com. All rights reserved.
// </copyright>
// -----------------------------------------------------------------------

using System;
using System.Data;

using Spaanjaars.ContactManager.Bll;
using Spaanjaars.ContactManager.BusinessEntities;
using Spaanjaars.ContactManager.Web;

public partial class AddEditContactPerson : BasePage
{
	private int _contactPersonId;

	protected void Page_Load(object sender, EventArgs e)
	{
		if (Request.QueryString.Get("Id") != null)
		{
			_contactPersonId = Convert.ToInt32(Request.QueryString.Get("Id"));
		}

		if (!Page.IsPostBack)
		{
			BindTypeDropDown();
			if (_contactPersonId > 0) // Edit an existing item
			{
				// Get person
				ContactPerson myContactPerson = ContactPersonManager.GetItem(_contactPersonId);
				if (myContactPerson != null)
				{
					LoadFormControlsFromContactPerson(myContactPerson);
				}
			}
			else
			{
				Title = "Create new Contact Person";
			}
		}
	}

	#region Load and Save Data

	private void SaveContactPerson()
	{
		if (calDateOfBirth.SelectedDate == DateTime.MinValue)
		{
			valRequiredDateOfBirth.IsValid = false;
		}
		if (Page.IsValid)
		{
			ContactPerson myContactPerson;
			if (_contactPersonId > 0)
			{
				// Update existing item
				myContactPerson = ContactPersonManager.GetItem(_contactPersonId);
			}
			else
			{
				// Create a new ContactPerson
				myContactPerson = new ContactPerson();
			}
			LoadContactPersonFromFormControls(myContactPerson);

			if (myContactPerson.Validate())
			{
				try
				{
					ContactPersonManager.Save(myContactPerson);
					EndEditing();
				}
				catch (DBConcurrencyException)
				{
					plcConcurrency.Visible = true;
				}
			}
			else
			{
				ErrorList1.BrokenRules = myContactPerson.BrokenRules;
			}
		}
	}

	private void LoadContactPersonFromFormControls(ContactPerson myContactPerson)
	{
		myContactPerson.FirstName = txtFirstName.Text;
		myContactPerson.MiddleName = txtMiddleName.Text;
		myContactPerson.LastName = txtLastName.Text;
		myContactPerson.DateOfBirth = calDateOfBirth.SelectedDate;
		myContactPerson.Type = (PersonType)Enum.Parse(typeof(PersonType), lstType.SelectedValue);
		myContactPerson.ConcurrencyId = (byte[])ViewState["ConcurrencyId"];
	}

	private void LoadFormControlsFromContactPerson(ContactPerson myContactPerson)
	{
		txtFirstName.Text = myContactPerson.FirstName;
		txtMiddleName.Text = myContactPerson.MiddleName;
		txtLastName.Text = myContactPerson.LastName;
		calDateOfBirth.SelectedDate = myContactPerson.DateOfBirth;
		if (lstType.Items.FindByValue(myContactPerson.Type.ToString()) != null)
		{
			lstType.Items.FindByValue(myContactPerson.Type.ToString()).Selected = true;
		}
		Title = string.Format("Edit {0}", myContactPerson.FullName);

		// Store the concurrency key in ViewState
		ViewState["ConcurrencyId"] = myContactPerson.ConcurrencyId;
	}

	#endregion

	#region Click Handlers

	protected void btnSave_Click(object sender, EventArgs e)
	{
		SaveContactPerson();
	}

	protected void btnCancel_Click(object sender, EventArgs e)
	{
		EndEditing();
	}

	protected void btnOverwrite_Click(object sender, EventArgs e)
	{
		// Get the updated ContactPerson and get its ConcurrencyId
		ContactPerson myContactPerson = ContactPersonManager.GetItem(_contactPersonId);
		ViewState["ConcurrencyId"] = myContactPerson.ConcurrencyId;
		SaveContactPerson();
	}

	protected void btnReload_Click(object sender, EventArgs e)
	{
		ContactPerson myContactPerson = ContactPersonManager.GetItem(_contactPersonId);
		LoadFormControlsFromContactPerson(myContactPerson);
	}

	#endregion

	#region Helpers

	private void EndEditing()
	{
		Response.Redirect("~/");
	}

	private void BindTypeDropDown()
	{
		lstType.DataSource = Helpers.GetPersonTypes();
		lstType.DataBind();
	}

	#endregion

}
